package com.jinfang.bankpractice.service;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.jinfang.bankpractice.dto.HisCount;
import com.jinfang.bankpractice.mapper.HistoryMapper;
import com.jinfang.bankpractice.mapper.QuesbankMapper;
import com.jinfang.bankpractice.mapper.RecordMapper;
import com.jinfang.bankpractice.model.History;
import com.jinfang.bankpractice.model.Record;

/***
 * 
 * @author zuo_ji
 *
 */
@Service
public class RecordService {
	
	private static Logger log=LoggerFactory.getLogger(RecordService.class);
	
	@Resource
	private RecordMapper recordMapper;
	
	@Resource
	private QuesbankMapper quesbankMapper;

	@Resource
	private HistoryMapper historyMapper;
	
	public HisCount getHiscount(Long userid,int courseid){
		int done=getDone(userid, courseid).size();
		int error=getError(userid, courseid).size();
		int total=(int) quesbankMapper.countByCourse(courseid);
		return new HisCount(total, done, error, total-done);
	}

	public boolean addorUpdate(final int score, String doneids, String errorids, long userid, int course) {
		Record record = recordMapper.getRecord(userid, course);
		History history = new History(userid, course, score, new Date());
		historyMapper.insertHistory(history);
		
		if (record == null) {
			record = new Record(userid, course, errorids, doneids, new Date());
			recordMapper.insertRecord(record);
			return true;
		}
		if (doneids != null && !doneids.equals("")) {
			Set<Long> doneSet = Arrays.asList(doneids.split(",")).stream().map(Long::parseLong)
					.collect(Collectors.toSet());
			doneSet.addAll(getDone(userid, course));
			String dones = doneSet.stream().sorted().map(a -> a + "").collect(Collectors.joining(","));
			record.setDoneids(dones);
		}

		if (errorids != null && !errorids.equals("")) {
			Set<Long> errorSet = Arrays.asList(errorids.split(","))
					.stream()
					.map(Long::parseLong)
					.collect(Collectors.toSet());
			errorSet.addAll(getError(userid, course));
			String errors = errorSet.stream().sorted().map(a -> a + "").collect(Collectors.joining(","));
			record.setErrorids(errors);
		}
		recordMapper.updateRecord(record);
		return true;
	}

	/**
	 * 做过的题
	 * 
	 * @param userid
	 * @param course
	 * @return
	 */
	public Set<Long> getDone(long userid, int course) {
		Record record = recordMapper.getRecord(userid, course);
		if (record == null || StringUtils.isEmpty(record.getDoneids())) {
			log.debug("don't find any done id,userid={},course={}",userid,course);
			return Collections.emptySet();
		}
		List<String> ids = Arrays.asList(record.getDoneids().split(","));
		return ids.stream().map(Long::parseLong).collect(Collectors.toSet());
	}

	/**
	 * 获取错题的id
	 * 
	 * @param userid
	 * @param course
	 * @return
	 */
	public Set<Long> getError(long userid, int course) {
		Record record = recordMapper.getRecord(userid, course);
		if (record == null || StringUtils.isEmpty(record.getErrorids())){
			log.debug("dont find error record,userid={},course={}",userid,course);
			return Collections.emptySet();
		}
		List<String> ids = Arrays.asList(record.getErrorids().split(","));
		return ids.stream().map(Long::parseLong).collect(Collectors.toSet());
	}

	/*public static void main(String[] args) {
		List<String> ids = Arrays.asList("2323,123213213".split(","));
		List<Long> aa = ids.stream().map(Long::parseLong).collect(Collectors.toList());
		System.out.println(aa);
	}*/

	/**
	 * 错题做对之后，将从题集集中移除出去
	 * 
	 * @param rightids
	 * @param id
	 * @param course
	 * @param userid
	 * @return
	 */
	public boolean reduceError(String rightids, Long id, int course, long userid) {
		Record record = recordMapper.getRecord(userid, course);
		if (!StringUtils.isEmpty(rightids)) {
			Set<Long> rightSet = Arrays.asList(rightids.split(",")).stream().map(Long::parseLong)
					.collect(Collectors.toSet());
			Set<Long> errorSet = getError(userid, course);
			errorSet.removeAll(rightSet);
			String errors = errorSet.stream().sorted().map(a -> a + "").collect(Collectors.joining(","));
			record.setErrorids(errors);
			recordMapper.updateRecord(record);
			return true;
		}
		log.error("not find any record,userid={},course={}",userid,course);
		return false;
	}

}
